package bbb.sequence;

import java.util.ArrayList;
import java.util.List;

import bbb.BallInfo;
import bbb.aaa.BlueSelector;
import bbb.ccc.Encoder;

public class DynomicAnalyze {

	public static void longStepScheme(int stopCount) {
		List<BallInfo> balls = Encoder.randomBallList(100);
		int day = 150;
		BlueSelector bs = new BlueSelector();

		AnalyzeResult result = new AnalyzeResult();
		List<SingleResult> results = new ArrayList<SingleResult>();
		result.setResults(results);

		boolean fired = false;
		List<BallInfo> bl = bs.longStepSelector(balls);
		BallInfo selectedBall = bl.get(0);
		int counter = 0;

		for (int i = 0; i < day; i++) {
			counter++;
			if (fired) {
				bl = bs.longStepSelector(balls);
//				System.out.println("===========" + balls.size());
				selectedBall = bl.get(0);
			}
			BallInfo cb = Encoder.randomBall();

			List<BallInfo> selectedBalls = copyBall(selectedBall, counter*3);
			SingleResult sr = new SingleResult(selectedBalls, cb);
			results.add(sr);
			
			if (selectedBall.getBlue() == cb.getBlue()) {
//				System.out.println(i + " - " + counter + " - " + cb.getBlue());
				fired = true;
				counter = 0;
			} else {
				fired = false;
				if( stopCount== counter){
					counter = 0;
				}
			}
			balls.add(cb);
		}

		System.out.println("****************************************");
//		result.printAllResult();
		result.printTotal();
	}
	
	public static void randomLongStepScheme(int stopCount) {
		List<BallInfo> balls = Encoder.randomBallList(100);
		int day = 150;

		AnalyzeResult result = new AnalyzeResult();
		List<SingleResult> results = new ArrayList<SingleResult>();
		result.setResults(results);

		boolean fired = false;
		BallInfo selectedBall = Encoder.randomBall();
		int counter = 0;

		for (int i = 0; i < day; i++) {
			counter++;
			if (fired) {
//				System.out.println("===========" + balls.size());
				selectedBall = Encoder.randomBall();
			}
			BallInfo cb = Encoder.randomBall();

			List<BallInfo> selectedBalls = copyBall(selectedBall, (int) Math
					.pow(2, counter-1));
			SingleResult sr = new SingleResult(selectedBalls, cb);
			results.add(sr);
			
			if (selectedBall.getBlue() == cb.getBlue()) {
//				System.out.println(i + " - " + counter + " - " + cb.getBlue());
				fired = true;
				counter = 0;
			} else {
				fired = false;
				if( stopCount== counter){
					counter = 0;
				}
			}
			balls.add(cb);
		}

		System.out.println("****************************************");
//		result.printAllResult();
		result.printTotal();
	}

	public static List<BallInfo> copyBall(BallInfo bi, int count) {
		List<BallInfo> bs = new ArrayList<BallInfo>();
		for (int i = 0; i < count; i++) {
			bs.add(bi);
		}
		return bs;
	}

	public static void main(String[] args) {
//		List<BallInfo> balls = Encoder.randomBallList(100);
//		// System.out.println(balls);
//		int day = 150;
//		BlueSelector bs = new BlueSelector();
//
//		boolean fired = false;
//		List<BallInfo> bl = bs.longStepSelector(balls);
//		BallInfo selectedBall = bl.get(0);
//		int counter = 0;
//		for (int i = 0; i < day; i++) {
//			counter++;
//			if (fired) {
//				bl = bs.longStepSelector(balls);
//				System.out.println("===========" + balls.size());
//				selectedBall = bl.get(0);
//			}
//			BallInfo cb = Encoder.randomBall();
//			if (selectedBall.getBlue() == cb.getBlue()) {
//				System.out.println(i + " - " + counter + " - " + cb.getBlue());
//				fired = true;
//				counter = 0;
//			} else {
//				fired = false;
//			}
//			balls.add(cb);
//		}
		longStepScheme(100);
//		for (int i = 1; i <= 12; i++){
//			longStepScheme(i);
//		}
//		randomLongStepScheme(i);
	}
}
